package com.auto.signup.service;

import com.auto.signup.entity.UserInfo;
import com.auto.signup.mapper.UserMapper;
import com.auto.signup.weixin.model.WebAuthTokenModel;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * TODO
 *
 * @author fl-041
 * @version 1.0
 * @date 2022/2/17 16:36
 */
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserInfo checkWinxin(WebAuthTokenModel tokenModel) {
        String loginMark = tokenModel.getOpenid();// 现在取的是OPENID 未来取 Unionid
        UserInfo userInfoExample = new UserInfo();
        userInfoExample.setOpenId(loginMark);
        UserInfo userInfo = userMapper.selectOne(userInfoExample);
        if(userInfo==null){
            userInfo = new UserInfo();
            userInfo.setOpenId(loginMark);
            userInfo.setCreateTime(new Date());
            userInfo.setId(UUID.randomUUID().toString());
            userInfo.setNickname(RandomStringUtils.randomAlphabetic(3));
            userMapper.insert(userInfo);
        }

        return userInfo;

    }
}
